import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
const daoName = "INDI_BXBL";
const ButtonGroup = Button.Group;
const columns = [
  { title: '导入结果', width: 250, sorter: true, dataIndex: 'DRJG', key: 'DRJG', dispaly: true, filter: true, export: false },
  { title: '年月', width: 160, sorter: true, dataIndex: 'YF', key: 'YF', dispaly: true, filter: true, export: false },
  { title: '投保日期', width: 120, sorter: true, key: 'TBRQ', caption: "TBRQ", render: data => datetime.format(data.TBRQ, "yyyy-MM-dd"), dispaly: true, filter: true, export: true },
  { title: '销售支行', width: 160, sorter: true, dataIndex: 'XSZH', key: 'XSZH', dispaly: true, filter: true, export: true },
  { title: '保险公司', width: 250, sorter: true, dataIndex: 'BXGS', key: 'BXGS', dispaly: true, filter: true, export: true },
  { title: '投保单号', width: 200, sorter: true, dataIndex: "TBDH", key: 'TBDH', dispaly: true, filter: true, export: true },
  { title: '投保人姓名', width: 160, sorter: true, dataIndex: "TBRXM", key: 'TBRXM', dispaly: true, filter: true, export: true },
  { title: '投保人证件号码', width: 160, sorter: true, dataIndex: "TBRZJHM", key: 'TBRZJHM', dispaly: true, filter: true, export: true },
  { title: '险种名称', width: 250, sorter: true, dataIndex: 'XZMC', key: 'XZMC', dispaly: true, filter: true, export: true },
  { title: '险种', width: 250, sorter: true, dataIndex: "XZ", key: 'XZ', dispaly: true, filter: true, export: true },
  { title: '首期保费（元）', width: 160, sorter: true, dataIndex: 'SQBF', key: 'SQBF', dispaly: true, filter: true, export: true },
  { title: '保险合同号（保单号）', width: 200, sorter: true, dataIndex: "BXHTH", key: 'BXHTH', dispaly: true, filter: true, export: true },
  { title: '合同生效日期', width: 160, sorter: true, key: 'HTSXRQ', caption: "HTSXRQ", render: data => datetime.format(data.HTSXRQ, "yyyy-MM-dd"), dispaly: true, filter: true, export: true },
  { title: '回执签收', width: 120, sorter: true, dataIndex: 'HZQS', key: 'HZQS', dispaly: true, filter: true, export: true },
  { title: '销售人', width: 120, sorter: true, dataIndex: "XSR", key: 'XSR', dispaly: true, filter: false, export: true },
  { title: '推荐人', width: 120, sorter: true, dataIndex: "TJR", key: 'TJR', dispaly: true, filter: false, export: true },
  { title: '趸交/期交', width: 140, sorter: true, dataIndex: 'DJ_QJ', key: 'DJ_QJ', dispaly: true, filter: false, export: true },
  { title: '是否犹退', width: 140, sorter: true, dataIndex: "SFYT", key: 'SFYT', dispaly: true, filter: false, export: true },
  { title: '购买渠道', width: 140, sorter: true, dataIndex: 'GMQD', key: 'GMQD', dispaly: true, filter: true, export: true },
  { title: '财产保险/人身保险', width: 180, sorter: true, dataIndex: "CCBX_RSBX", key: 'CCBX_RSBX', dispaly: true, filter: false, export: true },
  { title: '类型', width: 120, sorter: true, dataIndex: "LX", key: 'LX', dispaly: true, filter: false, export: true },
  { title: '备注', width: 120, sorter: true, dataIndex: "BZ", key: 'BZ', dispaly: true, filter: false, export: true },
  { title: '中收比率', width: 120, sorter: true, dataIndex: "ZSBL", key: 'ZSBL', dispaly: true, filter: false, export: true },
  { title: '中收', width: 120, sorter: true, dataIndex: "ZS", key: 'ZS', dispaly: true, filter: false, export: true },
  { title: '归属机构', width: 120, sorter: true, dataIndex: "GSJG", key: 'GSJG', dispaly: true, filter: false, export: true },
  { title: '销售金额（万元）', width: 150, sorter: true, dataIndex: "XSJE", key: 'XSJE', dispaly: true, filter: false, export: true },
  { title: '中收（万元）', width: 120, sorter: true, dataIndex: "ZSJE", key: 'ZSJE', dispaly: true, filter: false, export: true },
  { title: '业绩归属人', width: 120, sorter: true, dataIndex: "YJGSR", key: 'YJGSR', dispaly: true, filter: false, export: true },
];
const controls = [
  { name: 'YF', required: true, type: 'text', label: '年月', grid: 12 / 24, placeholder: "按YYYYMM格式输入,例如:202201", min: 6, max: 6 },
  { name: 'TBRQ', label: "投保日期", dataType: "date", type: "date", grid: 12 / 24 },
  { name: 'XSZH', type: 'text', label: '销售支行', grid: 12 / 24 },
  { name: 'BXGS', type: 'text', label: '保险公司', grid: 12 / 24 },
  { name: 'TBDH', label: "投保单号", type: "text", grid: 12 / 24 },
  { name: 'TBRXM', label: "投保人姓名", type: "text", grid: 12 / 24 },
  { name: 'TBRZJHM', label: '投保人证件号码', type: "text", grid: 12 / 24 },
  { name: 'XZMC', type: 'text', label: '险种名称', grid: 12 / 24 },
  { name: 'XZ', type: 'text', label: '险种（财产/人寿/年金/意外伤害/健康）', grid: 12 / 24 },
  { name: 'SQBF', label: "首期保费（元）", type: "text", grid: 12 / 24 },
  { name: 'BXHTH', type: 'text', label: '保险合同号（保单号）', grid: 12 / 24 },
  { name: 'HTSXRQ', label: "合同生效日期", dataType: "date", type: "date", grid: 12 / 24 },
  { name: 'HZQS', label: '回执签收', type: "text", grid: 12 / 24 },
  { name: 'XSR', type: 'text', label: '销售人', grid: 12 / 24 },
  { name: 'TJR', type: 'text', label: '推荐人', grid: 12 / 24 },
  { name: 'DJ_QJ', label: "趸交/期交（*年）", type: "text", grid: 12 / 24 },
  { name: 'SFYT', label: "是否犹退（是/否）", type: "text", grid: 12 / 24 },
  { name: 'GMQD', label: "购买渠道（银保通/线下单/网银/手机银行）", type: "text", grid: 12 / 24 },
  { name: 'CCBX_RSBX', label: '财产保险/人身保险', type: "text", grid: 12 / 24 },
  { name: 'LX', label: '类型（人寿/年金/意外伤害/健康）', type: "text", grid: 12 / 24 },
  { name: 'BZ', type: 'text', label: '备注', grid: 12 / 24 },
  { name: 'ZSBL', type: 'text', label: '中收比率', grid: 12 / 24 },
  { name: 'ZS', type: 'text', label: '中收', grid: 12 / 24 },
  { name: 'GSJG', type: 'text', label: '归属机构', grid: 12 / 24 },
  { name: 'XSJE', type: 'text', label: '销售金额（万元）', grid: 12 / 24 },
  { name: 'ZSJE', type: 'text', label: '中收（万元）', grid: 12 / 24 },
  { name: 'YJGSR', type: 'text', label: '业绩归属人', grid: 12 / 24 },
  { name: 'DRJG', required: false, ignore: true, type: 'text', label: '导入结果', grid: 12 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class BxtzForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
    });
    let sort = {
      field: 'YF',
      order: 'descend'
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 10, sorter: sort }
    });
    this.loadOptionData();
  }
  componentWillReceiveProps(nextProps) {
    const { showImport } = this.props
    if (!nextProps.showImport && showImport) {
      this.props.dispatch({
        type: `${daoName}/fetch`,
        payload: { daoName, page: 1, pageSize: 10 }
      });
    }
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showImportHandler() {
    this.props.dispatch({
      type: `${daoName}_DataImport/setDisplay`,
      payload: { display: true },
    });
  }
  DownTmportHandler() {
    let columnHeaders = []
    columns.map((c, j) => {
      if (!c.export) {
        return
      }
      let header = {
        Name: c.key,
        Caption: c.title,
        Width: c.width
      }
      columnHeaders.push(header)
    })
    let form = document.createElement('form');//定义一个form表单
    let whereItems = [];
    whereItems.push({
      FieldName: "1",
      Operator: "<>",
      FieldValues: "1"
    })
    const whereCauses = []
    whereCauses.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    const orderItems = []
    form.style = "display:none";
    form.target = "_blank";
    form.method = "post";
    form.action = "/api/ExportXlsx/" + daoName;
    let input1 = document.createElement("input");
    input1.type = "hidden";
    input1.name = "WhereCauses";
    input1.value = JSON.stringify(whereCauses);
    let input2 = document.createElement("input");
    input2.type = "hidden";
    input2.name = "OrderItems";
    input2.value = JSON.stringify(orderItems)
    let input3 = document.createElement("input");
    input3.type = "hidden";
    input3.name = "ColumnHeaders";
    input3.value = JSON.stringify(columnHeaders);
    let input4 = document.createElement("input");
    input4.type = "hidden";
    input4.name = "PermID";
    input4.value = "" + this.props.permID;
    let input5 = document.createElement("input");
    input5.type = "hidden";
    input5.name = "UserID";
    input5.value = "" + this.props.userID;
    document.body.appendChild(form);//将表单放置在web中
    form.appendChild(input1);
    form.appendChild(input2);
    form.appendChild(input3);
    form.appendChild(input4);
    form.appendChild(input5);
    form.submit();
    document.body.removeChild(form)
  }
  render() {
    let { showEdit, record, showImport, permID, popedomTypes } = this.props
    const pTypes = popedomTypes[permID]
    let data = null
    if (pTypes.indexOf("2") !== -1) {
      data = <ButtonGroup style={{ zIndex: 0 }}>
        <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
        <Button onClick={this.DownTmportHandler.bind(this)}>下载导入模板</Button>
      </ButtonGroup>
    }
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            {data}
          </div>
          <DataTable ref="BxtzTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
          <DataImport title="保险业务台账" importName="INDI_BXBL" daoName={daoName} paramNames={["YF"]} importMode="DeleteAfterInsert" controls={controls} forbidConvert={true} saveByDao={true} />
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  const { display } = state[`${daoName}_DataImport`];
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record,
    showImport: display
  }
}
BxtzForm = connect(mapStateToProps)(BxtzForm)
export default BxtzForm;

let render = (props) => React.createElement(BxtzForm, props);
let f = new Factory()
f.set("BxtzForm", render)
